﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for FrmPhase.xaml
    /// </summary>
    public partial class FrmPhase : Window
    {
        internal readonly static FrmPhase frmPhase = new FrmPhase();
        static bool result = false;
        #region Properties
        public string PhaseName { get { return this.txtPhaseName.Text; } set { this.txtPhaseName.Text = value; } }
        public string DefOfDone
        {
            get
            {
                return this.txtDefOfDone.IsEnabled ? this.txtDefOfDone.Text : null;
            }
            set { this.txtDefOfDone.Text = value; }
        }
        public int MaxOfFeature
        {
            get
            {
                return this.txtDefOfDone.IsEnabled ? (int)this.spMaxOfFeatures.Value : 0;
            }
            set { this.spMaxOfFeatures.Value = value; }
        }

        public List<Model.BasePhase> NextPhases
        {
            get
            {
                List<Model.BasePhase> res = new List<Model.BasePhase>();
                foreach (var item in listNextPhases.SelectedItems)
                {
                    res.Add((Model.BasePhase)item);
                }
                return res;
            }
        }
#endregion

        static public void setSource(IList<Model.BasePhase> list)
        {
            frmPhase.listNextPhases.ItemsSource = list;
        }
        
        public FrmPhase()
        {
            InitializeComponent();

            SetEvent();
        }
        
        private void ResetControl()
        {
            txtPhaseName.Text = "";
            txtDefOfDone.Text = "";
            spMaxOfFeatures.Value = 1;
            txtDefOfDone.IsEnabled = false;
            txtDefOfDone.Text = "Not necessary";
            spMaxOfFeatures.IsEnabled = false;
            spMaxOfFeatures.Text = "Not necessary";
        }
        private void AssignData(eKanbanWPF.Model.BasePhase basePhase)
        {
            if (basePhase != null)
            {
                this.PhaseName = basePhase.Name;                
                if (basePhase is eKanbanWPF.Model.Phase)
                {
                    eKanbanWPF.Model.Phase phase = basePhase as eKanbanWPF.Model.Phase;
                    txtDefOfDone.IsEnabled = true;
                    spMaxOfFeatures.IsEnabled = true;
                    this.DefOfDone = phase.DefinitionOfDone;
                    this.MaxOfFeature = phase.MaxFeature;
                }
                else
                {
                    txtDefOfDone.IsEnabled = false;
                    txtDefOfDone.Text = "Not necessary";
                    spMaxOfFeatures.IsEnabled = false;
                    spMaxOfFeatures.Text = "Not necessary";

                }
                listNextPhases.UnSelectAll();
                foreach (var item in basePhase.NextPhases)
                {
                    listNextPhases.SelectedItems.Add(item);
                }
            }
            else
            {
                ResetControl();
            }
        }

        public static bool Show(eKanbanWPF.Model.BasePhase phase)
        {
            result = false;
            frmPhase.AssignData(phase);
            var r = frmPhase.ShowDialog();
            return result;
        }

        private void SetEvent()
        {
            App.Current.MainWindow.Closed += (sender, e) => this.Close();

            this.Closing += new System.ComponentModel.CancelEventHandler(FrmPhase_Closing);

            this.btnOK.Click += new RoutedEventHandler(btnOK_Click);
            this.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);

            this.txtPhaseName.Validate += new DevExpress.Xpf.Editors.Validation.ValidateEventHandler(txtPhaseName_Validate);
            this.txtDefOfDone.Validate += new DevExpress.Xpf.Editors.Validation.ValidateEventHandler(txtDefOfDone_Validate);
        }
        
        #region Events
        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                result = true;
                e.Handled = true;
                this.DialogResult = true;
            }
        }

        void FrmPhase_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.Visibility == System.Windows.Visibility.Visible)
            {
                this.Hide();
                e.Cancel = true;
            }

        }

        void txtDefOfDone_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            var value = e.Value;

            e.IsValid = true;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                e.IsValid = false;
                e.ErrorContent = "Definition of done cannot be empty.";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }
        }

        void txtPhaseName_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            var value = e.Value;

            e.IsValid = true;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                e.IsValid = false;
                e.ErrorContent = "Phase name cannot be empty.";                
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }
        }
        #endregion

        private bool Validate()
        {
            return true;
        }
    }
}
